package com.example.commonlib.base;
import com.example.commonlib.ResourceTable;
import com.example.commonlib.base.presenter.SuperPresenter;
import com.example.commonlib.base.view.SuperAbility;
import ohos.agp.components.*;
import ohos.app.Context;


/**
 * Activity 顶部可添加一个标准的 toolbar，只需在 xml 中添加一个 @+id = toolbar 的 Toolbar 即可，样式需自定义
 * <p>
 * Created by linlongxin on 2016/7/31.
 */

public class ToolbarAbility<T extends SuperPresenter> extends SuperAbility<T> {
    private boolean isHomeBack = true;
    private int visibility=0;
    // 右侧按钮的显示隐藏，默认显示
    private int rightVisibility=0;
    private Component mToolbar;
    private Text title;
    private Image btnBack;
    private Button btnRight;
    private int icon;

    /**
     * setToolbarHomeBack
     *
     * @param isHomeBack
     */
    public void setToolbarHomeBack(boolean isHomeBack) {
        this.isHomeBack = isHomeBack;
        //转换参数
        this.visibility = isHomeBack ? 0 : 1;
        // 设置 返回的显示隐藏
        btnBack.setVisibility(visibility);
    }

    @Override
    public void setUIContent(ComponentContainer componentContainer) {
        // toolbar
        mToolbar = (Component) componentContainer.findComponentById(ResourceTable.Id_toolbar);
        // title
        title = (Text) mToolbar.findComponentById(ResourceTable.Id_toolbar_title);
        // btnBack
        btnBack = (Image) mToolbar.findComponentById(ResourceTable.Id_btn_back);
        // 绑定左侧按钮事件
        bindBackEvent();
        // 右侧按钮
        btnRight = (Button) mToolbar.findComponentById(ResourceTable.Id_btn_right);
        // 绑定右侧按钮事件
        bindRightEvent();

        mRealContent = componentContainer;
        if (isUseStatusPages) { //添加状态页到activity
            DirectionalLayout directionalLayout = new DirectionalLayout(this);
            directionalLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
            directionalLayout.setOrientation(DirectionalLayout.VERTICAL);

            // 判断是否已有 mToolbar -remove
            if (mToolbar != null) {
                if (mToolbar.getComponentParent() != null && mToolbar.getComponentParent() instanceof ComponentContainer) {
                    ((ComponentContainer) mToolbar.getComponentParent()).removeComponent(mToolbar);
                }
            }
            // 重新添加 mToolbar
            directionalLayout.addComponent(mToolbar);

            // 获取状态页
            Context context=getApplicationContext();
            LayoutScatter getLayoutInflater = LayoutScatter.getInstance(context);
            ComponentContainer root = (ComponentContainer)getLayoutInflater.parse(ResourceTable.Layout_base_status_page, null, false);

            // 而是直接将 mRealContent 添加到root 状态页
            root.addComponent(mRealContent);

            // 将root 生成的布局
            directionalLayout.addComponent(root);

            //显示状态页-有用这里未使用顶层窗口addcomponet,所以使用继承的setUicontent方式显示页面
            setTitleBarContentView(directionalLayout);

            // 初始相关组件等
            initStatusPages(directionalLayout);
        }
        else{
            setTitleBarContentView(componentContainer);
        }

    }

    public void initStatusPages(ComponentContainer parent) {
        mEmptyPage = (DirectionalLayout) parent.findComponentById(ResourceTable.Id_empty_page);
        mLoadDataButton = (DirectionalLayout) parent.findComponentById(ResourceTable.Id_error_to_load_button);
        mErrorPage = (DirectionalLayout)parent.findComponentById(ResourceTable.Id_error_page);
        mLoadingPage = (DirectionalLayout) parent.findComponentById(ResourceTable.Id_loading_page);
        mRealContent.setVisibility(Component.HIDE);
        mCurrentPage = mLoadingPage;
        // 点击重新加载
        mLoadDataButton.setClickedListener(new Component.ClickedListener() {

            @Override
            public void onClick(Component v) {
                onErrorRetry(v);
            }
        });
    }




    /**
     * set Title
     *
     * @param title
     */
    public void setTitle(int title) {
        this.title.setText(title);
    }

    /**
     * set Left img
     *
     * @param icon
     */
    public void setLeftIcon(int icon) {
        if(icon==0){
            this.btnBack.setVisibility(0);
        }
        this.btnBack.setPixelMap(icon);
    }

    /**
     * set right text
     *
     * @param text
     */
    public void setRightText(int text) {
        this.btnRight.setText(text);
    }

    /**
     * 设置右侧按钮的显示隐藏
     *
     * @param showRight
     */
    public void setRightVisibility(Boolean showRight) {
        //转换参数
        this.rightVisibility = showRight ? 0 : 1;
        // 设置 返回的显示隐藏
        btnRight.setVisibility(this.rightVisibility);
    }

    /**
     * 绑定 back event
     */
    public void bindBackEvent() {
        // 点击返回
        btnBack.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                backEvent();
            }
        });
    }

    // 返回
    public void backEvent() {
        System.out.println("back");
    }

    /**
     * 绑定 right event
     */
    public void bindRightEvent() {
        // 点击返回
        btnRight.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                rightEvent();
            }
        });
    }

    // 返回
    public void rightEvent() {
        System.out.println("right");
    }


}